package com.hnucm.cooklife.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hnucm.cooklife.R;
import com.hnucm.cooklife.adapter.CollectAdapter;
import com.hnucm.cooklife.adapter.RecomAdapter;
import com.hnucm.cooklife.base.BaseFragment;
import com.hnucm.cooklife.base.utils.LoginUtils;
import com.hnucm.cooklife.bean.base.Event;
import com.hnucm.cooklife.bean.db.Dish;
import com.hnucm.cooklife.bean.db.History;
import com.hnucm.cooklife.bean.me.DishColl;
import com.hnucm.cooklife.contract.history.Contract;
import com.hnucm.cooklife.presenter.history.HistoryPresenter;
import com.hnucm.cooklife.ui.activity.DishDetailActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CoHisFragment extends BaseFragment<Contract.IHistoryView, HistoryPresenter> implements Contract.IHistoryView ,
        com.scwang.smartrefresh.layout.listener.OnLoadMoreListener,
        com.scwang.smartrefresh.layout.listener.OnRefreshListener{
    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        mPresenter.loadHistory(userId);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mPresenter.loadHistory(userId);
    }

    private Context mContext;
    private CollectAdapter mAdapter;
    private List<DishColl> mDishList = new ArrayList<>();

    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mSmartRefreshLayout;

    private int userId;

    @BindView(R.id.codish_recycler)
    RecyclerView mRecyclerView;

    @Override
    protected int getContentViewId() {
        return R.layout.co_dish_fragment;
    }

    @Override
    protected void init() {
        mContext = getContext().getApplicationContext();
        initAdapter();
        userId = LoginUtils.getLoginId();
        mPresenter.loadHistory(userId);

        mSmartRefreshLayout.setNestedScrollingEnabled(false);
        mSmartRefreshLayout.setOnLoadMoreListener(this);
        mSmartRefreshLayout.setOnRefreshListener(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    private void initAdapter() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
        mAdapter = new CollectAdapter(mContext,mDishList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new CollectAdapter.onItemClickListener() {
            @Override
            public void onSeleClickListener(View view, int position) {
                Intent intent = new Intent(getActivity(), DishDetailActivity.class);
                intent.putExtra("dishId",mDishList.get(position).dishId);
                DishColl dish = mDishList.get(position);

                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }

    @Override
    protected HistoryPresenter createPresenter() {
        return new HistoryPresenter();
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onLoadingFailed() {

    }

    @Override
    public void onLoadSuccess() {
        mSmartRefreshLayout.finishLoadMore();
        mSmartRefreshLayout.finishRefresh();
    }

    @Override
    public void onLoadHistory(List<DishColl> historyList) {
        mDishList.clear();
        mDishList.addAll(historyList);
        mAdapter.setDishList(mDishList);
    }

    @Override
    public void onRefreshHistory(List<DishColl> historyList) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event){
        if(event.target == Event.TARGET_HISTORY){
            if(event.type == Event.TYPE_UPDATE_HISTORY){
                Integer dishId = Integer.valueOf(event.data);
                DishColl dishColl1 = mDishList.stream().filter(dishColl -> dishColl.dishId == dishId)
                        .findFirst().get();
                mDishList.remove(dishColl1);
                mDishList.add(0,dishColl1);
                mAdapter.notifyDataSetChanged();
                mPresenter.loadHistory(userId);
            }
        }
    }
}
